home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / General / PTR-TCL v2.1 / 2) TCL Munger / Munge.c < prev    next >
Text File  |  1994-02-17  |  5KB  |  224 lines

  1. /*
  2.  * munge.c
  3.  */
  4.  
  5. extern void Idle ( void ) ;
  6. extern short vRefNum ;
  7. void MungeFolder ( long parID ) ;
  8. extern void Message ( unsigned char * ) ;
  9. extern Boolean DoLine ( unsigned char * line ) ;
  10.  
  11.  
  12. CInfoPBRec rec ;
  13. short err ;
  14. Str63 name ;
  15. Str255 str ;
  16. short lineNo ;
  17.  
  18. static void
  19. CopyP ( unsigned char * from , unsigned char * to ) {
  20.     BlockMove ( from , to , 1 + * from ) ;
  21. }
  22.  
  23.  
  24. static void
  25. ConcP ( unsigned char * to , unsigned char * tail ) {
  26.  
  27. short len = * tail ;
  28.     if ( len + * to > 255 ) {
  29.         len = 255 - * to ;
  30.     }
  31.     BlockMove ( tail + 1 , to + * to + 1 , len ) ;
  32.     * to += len ;
  33. }
  34.  
  35.  
  36. static void
  37. ErrMsg ( unsigned char * why , unsigned char * file , short code ) {
  38.  
  39. Str15 nn ;
  40. long tick ;
  41. static long lastBeep = 0 ;
  42.  
  43.     NumToString ( code , nn ) ;
  44.     CopyP ( "\p ERROR " , str ) ;
  45.     ConcP ( str , why ) ;
  46.     ConcP ( str , "\p " ) ;
  47.     ConcP ( str , nn ) ;
  48.     ConcP ( str , "\p\r" ) ;
  49.     Message ( str ) ;
  50.     tick = TickCount ( ) + 30 ;
  51.     if ( lastBeep < tick - 600 ) { /* Only beep every 10 seconds if lots of errors */
  52.         SysBeep ( 20 ) ;
  53.         lastBeep = tick ;
  54.     }
  55.     do {
  56.         Idle ( ) ;
  57.     } while ( TickCount ( ) < tick ) ;
  58. }
  59.  
  60.  
  61. static long
  62. GetLine ( Handle text , long pos , unsigned char * into ) {
  63.  
  64. long end = GetHandleSize ( text ) ;
  65.  
  66.     lineNo ++ ;
  67.     * into = 0 ;
  68.     while ( pos < end ) {
  69.         if ( * into == 255 ) {
  70.             ErrMsg ( "\pLong Line!" , "\pLine" , lineNo ) ;
  71.             return pos ;
  72.         }
  73.         ( * into ) ++ ;
  74.         into [ * into ] = ( * text ) [ pos ] ;
  75.         pos ++ ;
  76.         if ( into [ * into ] == '\r' ) {
  77.             return pos ;
  78.         }
  79.     }
  80.     return pos ;
  81. }
  82.  
  83.  
  84. static long
  85. PutLine ( Handle text , long pos , long len , unsigned char * str ) {
  86.     ConcP ( str , "\p// " ) ; /* Comment out old line */
  87.     Munger ( text , pos , NULL , 0L , str + 1 , * str ) ;
  88.     return pos + len + * str ;
  89. }
  90.  
  91.  
  92. /* This function contains the total text of a TEXT file. */
  93. /* The contents of the handle will be re-written to disk when done. */
  94. /* Call Idle ( ) now and then for lengthy tasks */
  95. static void
  96. MungeHandle ( Handle text ) {
  97.  
  98. Str255 line ;
  99. long curLine = 0 ;
  100. long lineLen ;
  101. int ix = 20 ;
  102.  
  103.     lineNo = 0 ;
  104.     while ( curLine < GetHandleSize ( text ) ) {
  105.         ix -- ;
  106.         if ( ! ix ) {
  107.             Idle ( ) ;
  108.             ix = 20 ;
  109.         }
  110.         lineLen = GetLine ( text , curLine , str ) - curLine ;
  111.         if ( lineLen != str [ 0 ] ) {
  112. //            Message ( "\plineLen in MungeHandle wrong" ) ; /* Should be equal! */
  113.         }
  114.         if ( DoLine ( str ) ) {
  115.             curLine = PutLine ( text , curLine , lineLen , str ) ;
  116.         } else {
  117.             curLine += lineLen ;
  118.         }
  119.     }
  120. }
  121.  
  122.  
  123. static void
  124. MungeFile ( short vRef , long parID , unsigned char * fn ) {
  125.  
  126. Handle text ;
  127. short ref = 0 ;
  128. long size ;
  129.  
  130.     Message ( fn ) ;
  131.  
  132.     err = HOpenDF ( vRef , parID , fn , fsRdWrPerm , & ref ) ;
  133.     if ( err ) {
  134.         ErrMsg ( "\pOpening" , fn , err ) ;
  135.         return ;
  136.     }
  137.     err = GetEOF ( ref , & size ) ;
  138.     if ( ! err ) {
  139.         err = SetFPos ( ref , fsFromStart , 0L ) ;
  140.     }
  141.     if ( err ) {
  142.         ErrMsg ( "\pStarting" , fn , err ) ;
  143.         FSClose ( ref ) ;
  144.         return ;
  145.     }
  146.     text = NewHandle ( size ) ;
  147.     if ( ! text ) {
  148.         ErrMsg ( "\pAllocating" , fn , MemError ( ) ) ;
  149.         FSClose ( ref ) ;
  150.         return ;
  151.     }
  152.     HLock ( text ) ;
  153.     err = FSRead ( ref , & size , * text ) ;
  154.     if ( err ) {
  155.         ErrMsg ( "\pReading" , fn , err ) ;
  156.         FSClose ( ref ) ;
  157.         DisposeHandle ( text ) ;
  158.     }
  159.     HUnlock ( text ) ;
  160.     MungeHandle ( text ) ;
  161.     size = GetHandleSize ( text ) ;
  162.     err = SetEOF ( ref , size ) ;
  163.     if ( ! err ) {
  164.         err = SetFPos ( ref , fsFromStart , 0L ) ;
  165.     }
  166.     if ( err ) {
  167.         ErrMsg ( "\pFinishing" , fn , err ) ;
  168.         FSClose ( ref ) ;
  169.         DisposeHandle ( text ) ;
  170.         return ;
  171.     }
  172.     HLock ( text ) ;
  173.     err = FSWrite ( ref , & size , * text ) ;
  174.     if ( err ) {
  175.         ErrMsg ( "\pWriting" , fn , err ) ;
  176.         FSClose ( ref ) ;
  177.         DisposeHandle ( text ) ;
  178.         return ;
  179.     }
  180.     err = FSClose ( ref ) ;
  181.     if ( ! err ) {
  182.         err = FlushVol ( NULL , vRef ) ;
  183.     }
  184.     DisposeHandle ( text ) ;
  185.     if ( err ) {
  186.         ErrMsg ( "\pClosing" , fn , err ) ;
  187.     }
  188.     Message ( "\p OK\r" ) ;
  189. }
  190.  
  191.  
  192. void
  193. MungeFolder ( long parID ) {
  194.  
  195. short ix ;
  196.  
  197.     for ( ix = 1 ; ix > 0 ; ix ++ ) {
  198.         rec . hFileInfo . ioCompletion = 0L ;
  199.         rec . hFileInfo . ioFVersNum = 0 ;
  200.         rec . hFileInfo . ioNamePtr = name ;
  201.         rec . hFileInfo . ioVRefNum = vRefNum ;
  202.         rec . hFileInfo . ioDirID = parID ;
  203.         rec . hFileInfo . ioFDirIndex = ix ;
  204.         err = PBGetCatInfoAsync ( & rec ) ;
  205.         do {
  206.             Idle ( ) ;
  207.         } while ( rec . hFileInfo . ioResult == 1 ) ;
  208.         err = rec . hFileInfo . ioResult ;
  209.         if ( err ) {
  210.             break ;
  211.         }
  212.         if ( rec . hFileInfo . ioFlAttrib & 0x10 ) { /* Folder */
  213.             CopyP ( "\p\r•Entering Folder " , str ) ;
  214.             ConcP ( str , name ) ;
  215.             ConcP ( str , "\p\r" ) ;
  216.             Message ( str ) ;
  217.             MungeFolder ( rec . hFileInfo . ioDirID ) ;
  218.             Message ( "\p---- Leaving folder ----\r\r" ) ;
  219.         } else if ( rec . hFileInfo . ioFlFndrInfo . fdType == 'TEXT' ) {
  220.             MungeFile ( vRefNum , parID , name ) ;
  221.         }
  222.     }
  223. }
  224.